package com.timebank.score.controller;

import com.timebank.common.core.web.controller.BaseController;
import com.timebank.common.core.web.page.TableDataInfo;
import com.timebank.common.core.web.pojo.AjaxResult;
import com.timebank.common.security.annotation.RequiresLogin;
import com.timebank.score.pojo.entity.Goods;
import com.timebank.score.pojo.vo.GoodsVO;
import com.timebank.score.service.GoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 商品 控制层
 * @author shiliuyinzhen
 */
@RestController
@RequestMapping("/goods")
@Api(tags = "商品相关接口")
public class GoodsController extends BaseController {

    @Resource
    private GoodsService goodsService;


    /**
     * 新增商品
     * @param goods
     * @return
     */
    @RequiresLogin
    @PostMapping
    @ApiOperation("新增商品")
    public AjaxResult addGoods(@RequestBody Goods goods){
        return goodsService.addGoods(goods);
    }

    /**
     * 获取商品列表
     * @param goods
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("获取商品列表")
    public TableDataInfo getGoodsList(Goods goods){
        startPage();
        List<GoodsVO> list = goodsService.getGoodsList(goods);
        return getDataTable(list);
    }

    /**
     * 批量删除商品
     * @param goodsIds
     * @return
     */
    @RequiresLogin
    @DeleteMapping("/{goodsIds}")
    @ApiOperation("批量删除商品")
    public AjaxResult batchDeleteGoods(@PathVariable("goodsIds") Long[] goodsIds){
        return goodsService.batchDeleteGoods(goodsIds);
    }

    /**
     * 根据Id修改商品信息
     * @param goods
     * @return
     */
    @RequiresLogin
    @PutMapping("/{goodsId}")
    @ApiOperation("根据Id修改商品信息")
    public AjaxResult updateGoods(@PathVariable("goodsId")Long goodsId,@RequestBody Goods goods){
        return goodsService.updateGoodsById(goodsId,goods);
    }

    /**
     * 兑换商品
     * @param goodsId
     * @return
     */
    @RequiresLogin
    @PostMapping("/exchange/{goodsId}")
    @ApiOperation("兑换商品")
    public AjaxResult exchangeGoods(@PathVariable("goodsId")Long goodsId){
        return goodsService.exchangeGoods(goodsId);
    }
}
